package api

import (
	"gitee.com/linxing_3/sr-core/domain/api"
	"gitee.com/mkwchecking/sr-admin/apps/assets/acl"
	"gitee.com/mkwchecking/sr-admin/apps/assets/service"
	"github.com/gin-gonic/gin"
)

func NewDirScan(scanSrv *service.AssetsDirScanner) *AssetsDirScan {
	return &AssetsDirScan{
		scanSrv: scanSrv,
	}
}

// AssetsDirScan 目录扫描器
type AssetsDirScan struct {
	scanSrv *service.AssetsDirScanner
}

// ScanLocalDir 扫描本地目录
// @Summary 扫描本地目录
// @Tags AssetsScan
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param req body api.ScanLocalDirReq true "请求参数"
// @Success 200 {object} api.Response
// @Router /api/v1/assets/scan/local [post]
func (s *AssetsDirScan) ScanLocalDir(c *gin.Context) {
	req := &acl.AssetsLocalScanReq{}

	apiCtx := api.NewCtx(c).
		WithReq(req)

	if !apiCtx.Do() {
		return
	}

	err := s.scanSrv.ScanDir(apiCtx.Context(), req)

	if err != nil {
		apiCtx.WriteErr(nil, err, "扫描请求发送失败")
		return
	}

	apiCtx.WriteOK(nil, "扫描请求发送成功")
}

// GetLocalScanState 获取本地扫描状态
// @Summary 获取本地扫描状态
// @Tags AssetsScan
// @Produce json
// @Param Authorization header string true "Authorization"
// @Success 200 {object} api.Response
// @Router /api/v1/assets/scan/local/state [get]
func (s *AssetsDirScan) GetLocalScanState(c *gin.Context) {
	apiCtx := api.NewCtx(c)

	if !apiCtx.Do() {
		return
	}

	state := s.scanSrv.GetScanState(apiCtx.Context())

	apiCtx.WriteOK(state, "获取扫描状态成功")
}
